package com.nd.mobile.study.common.util;

import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;

import com.nd.mobile.study.domain.User;

public class SharedPreferencesUtil {

	/**
	 * 登陆界面的shared文件名
	 */
	public static final String	SHARED_LOGIN				= "login";

	/**
	 * UserInfo的shared文件名
	 */
	public static final String	SHARED_USER_INFO			= "userInfo";

	/**
	 * HistoryUserInfo的shared文件名
	 */
	public static final String	SHARED_HISTORY_USER_INFO	= "historyUserInfo";

	/**
	 * 系统配置的shared文件名
	 */
	public static final String	SHARED_SYS_CONFIG			= "syscfg";

	/**
	 * 用户消息配置
	 */
	public static final String	SHARED_USER_MSG				= "userMsg";

	/**
	 * 用户视频播放信息
	 */
	private static final String	SHARED_USER_VIDEO_INFO	= "userVideoInfo";

	SharedPreferences			sp;
	SharedPreferences.Editor	editor;

	Context						context;

	/**
	 * 以指定名字来创建
	 * 
	 * @param c
	 * @param name
	 */
	private SharedPreferencesUtil(Context c, String name) {
		context = c;
		sp = context.getSharedPreferences(name, 0);
		editor = sp.edit();
	}
	
	public static SharedPreferencesUtil getUserViedoInfoSp(Context c, User user) {
		return new SharedPreferencesUtil(c, SHARED_USER_VIDEO_INFO+((null == user)?0:user.getUserId()));
	}

	public static SharedPreferencesUtil getUserInfoSp(Context c) {
		return new SharedPreferencesUtil(c, SHARED_USER_INFO);
	}

	public static SharedPreferencesUtil getHistoryUserInfoSp(Context c) {
		return new SharedPreferencesUtil(c, SHARED_HISTORY_USER_INFO);
	}

	public static SharedPreferencesUtil getSyscfgSp(Context c) {
		return new SharedPreferencesUtil(c, SHARED_SYS_CONFIG);
	}

	public static SharedPreferencesUtil getUserMsgSp(Context c) {
		return new SharedPreferencesUtil(c, SHARED_USER_MSG);
	}

	/**
	 * 以键值<String Key,boolean Value> 的方式加入数据
	 * 
	 * @param key
	 * @param value
	 */
	public void putBooleanValue(String key, boolean value) {
		editor = sp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 以键值<String Key,String Value> 的方式加入数据
	 * 
	 * @param key
	 * @param value
	 */
	public void putValue(String key, String value) {
		editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 以键值<String Key,int Value> 的方式加入数据
	 * 
	 * @param key
	 * @param value
	 */
	public void putIntValue(String key, int value) {
		editor = sp.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * 以键值<String Key,long Value> 的方式加入数据
	 * 
	 * @param key
	 * @param value
	 */
	public void putLongValue(String key, long value) {
		editor = sp.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * 以 boolean Key 为索引来取出数据
	 * 
	 * @param key
	 * @return
	 */
	public boolean getBoolean(String key) {
		return sp.getBoolean(key, false);
	}

	/**
	 * 以 String Key 为索引来取出数据
	 * 
	 * @param key
	 * @return
	 */
	public String getValue(String key) {
		return sp.getString(key, null);
	}

	/**
	 * 以 int Key 为索引来取出数据
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key) {
		return sp.getInt(key, 0);
	}

	/**
	 * 以 long Key 为索引来取出数据
	 * 
	 * @param key
	 * @return
	 */
	public long getLong(String key) {
		return sp.getLong(key, 0);
	}

	/**
	 * 判断key键值是否存在
	 * 
	 * @param key
	 * @return
	 */
	public boolean contains(String key) {
		return sp.contains(key);
	}

	/**
	 * 移除每个键值
	 * 
	 * @param key
	 */
	public void remove(String key) {
		editor = sp.edit();
		editor.remove(key);
		editor.commit();
	}

	/**
	 * 删除所有
	 */
	public void clear() {
		editor = sp.edit();
		editor.clear();
		editor.commit();
	}

	/**
	 * 更新
	 * 
	 * @param spu
	 * @param params
	 */
	@SuppressWarnings("rawtypes")
	public void update(SharedPreferencesUtil spu, Map params) {
		Iterator keyValuePairs1 = params.entrySet().iterator();
		while (keyValuePairs1.hasNext()) {
			Map.Entry entry = (Map.Entry) keyValuePairs1.next();
			String key = entry.getKey().toString();
			String value = entry.getValue().toString();
			if (spu.contains(key)) {
				if (!value.equals(spu.getValue(key))) {
					spu.putValue(key, value);
				}
			} else {
				spu.putValue(key, value);
			}

		}
	}

}
